package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 菜品模块
@Api(tags = "菜品模块")
@RestController
public class DishController {



    @Autowired //注入
    private DishService dishService;
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页个数",required = true),
            @ApiImplicitParam(name = "name",value = "可能模糊查询的名字",required = false),
    })
    @GetMapping("/dish/page") // 前端是get请求 分页查询用 @RequestParam
    //                           defaultValue = "1"  是默认值 当前端不传参数的咱们就可以指定他
    public ResultInfo findByPige(@RequestParam(name = "page" ,defaultValue = "1") Integer PageNum,
                                 @RequestParam(name = "pageSize") Integer PageSize,
                                 String name){ // name这个参数是因为页面有一个模糊查询 所以要加这个参数
        // 接受完参数以后调用方法
       Page<Dish> dishPage= dishService.findByPige(PageNum,PageSize,name);

       // 返回结果
       return ResultInfo.success(dishPage);
    }

    //新增
    @ApiOperation("新增")
    @ApiImplicitParam(name = "dish",value = "新增的菜品",required = true)
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){ // 接收参数
        // 调用方法
        dishService.save(dish);
        // 返回成功
        return ResultInfo.success();
    }

    // 修改回显
    @ApiOperation("修改回显")
    @ApiImplicitParam(name = "id",value = "修改回显",required = true)
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){ // 接受参数
        // 调用方法
        Dish dishList=dishService.findById(id);

        return ResultInfo.success(dishList);
    }

    // 修改
    @ApiOperation("修改")
    @PutMapping("/dish")
    @ApiImplicitParam(name = "dish",value = "修改的菜品",required = true)
    public ResultInfo update(@RequestBody Dish dish){ //接收参数
        // 调用修改
    dishService.update(dish);
    // 返回结果
        return ResultInfo.success();
    }
    // 删除
    @DeleteMapping("/dish")
    @ApiOperation("删除")
    @ApiImplicitParam(name = "ids",value = "批量删除的id",required = true)
    public ResultInfo delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return ResultInfo.success();
    }


    // 停售启用
    @ApiImplicitParams({
     @ApiImplicitParam(name = "status",value = "状态",required = true),
     @ApiImplicitParam(name = "ids",value = "要修改的状态码",required = true)
    })
    @ApiOperation("停售启用")
   @PostMapping("/dish/status/{status}")//路径传参
    public ResultInfo updateStatus(@PathVariable Integer status,
                                   @RequestParam List<Long> ids ){ //
        dishService.updateStatus(status,ids);
        return ResultInfo.success();
   }



    //回显
    @GetMapping("/dish/list")
    public ResultInfo findByList(Long categoryId,String name){// 接受参数 前端是get请求 不需要加注解又是？传参
        //  调用service方法
        List<Dish> dishList= dishService.findByList(categoryId ,name);
        // 返回结果
        return ResultInfo.success(dishList);
    }


}
